package com.cskaoyan.service.wx.sl;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.vo.wx.sl.WxSearchIndexVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    KeywordMapper keywordMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    //显示搜索keyword表信息
    @Override
    public WxSearchIndexVo searchIndex() {
        //默认的
        Keyword keyword = keywordMapper.selectByPrimaryKey(1);
        //先查全部，在筛选剩余的
        List<Keyword> list = keywordMapper.queryKey();
        List<Keyword> arrayList = new ArrayList<>();
        for (Keyword key : list) {
            Integer id = key.getId();
            if (id != 1){
                arrayList.add(key);
            }
        }
        //查询历史
        List<SearchHistory> historyList = searchHistoryMapper.queryHistory();
        List<SearchHistory> aaa = new ArrayList<>();
        for (SearchHistory searchHistory : historyList) {
            //判断状态，看是否要传回去
            Boolean deleted = searchHistory.getDeleted();
            if (deleted != true){
                aaa.add(searchHistory);
            }
        }
        //封装
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo(keyword,arrayList,aaa);
        return wxSearchIndexVo;
    }
    //搜索帮助
    @Override
    public List searchHelper(String keyword) {
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike("%"+keyword+"%");
        List<Keyword> list = keywordMapper.selectByExample(keywordExample);
        List<String> arrayList = new ArrayList<>();
        for (Keyword keyword1 : list) {
            String name = keyword1.getKeyword();
            arrayList.add(name);
        }
        return arrayList;
    }
    //清空历史足迹
    @Override
    public int searchClearhistory() {
        User usere = (User) SecurityUtils.getSubject().getPrincipal();
        Integer usereId = usere.getId();
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(usereId);
        List<SearchHistory> historyList = searchHistoryMapper.selectByExample(searchHistoryExample);
        SearchHistory bean = new SearchHistory();
        for (SearchHistory sear : historyList) {
            Boolean deleted = sear.getDeleted();
            deleted = true;
            int i = searchHistoryMapper.updateDeleted(deleted);
        }
        return 200;
    }
}
